﻿<%@ WebHandler Language="C#" Class="CBDashbord" %>

using System;
using System.Web;
using Chenxin.Lib.Utility;
using System.Data;
using OpenFlashChart;
using Bar = OpenFlashChart.Bar;
using ToolTip = OpenFlashChart.ToolTip;
using YAxis = OpenFlashChart.YAxis;
using System.Collections.Generic;

public class CBDashbord :  AjaxHandlerBase
{
    override public void RequestProcess(HttpContext context)
    {
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";

        string op = context.Request.Params["op"];
        string strRet = string.Empty;
        try
        {
            switch (op)
            {
                case "getpiedb":
                    GetPieDB(context);
                    break;
                case "getglassbardb":
                    GetGlassBarDB(context);
                    break;
            }
        }
        catch (Exception ex)
        {
            context.Response.Write("程序执行失败！原因：" + ex.Message);
            //strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            //context.Response.Write(strRet);
        }
    }

    /// <summary>
    /// 获取报表数据源
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    private DataTable GetData(HttpContext context, ref int NodeType)
    {
        string strRet = string.Empty;
        string strParam = string.Empty;
        string ParentType = string.Empty;
        string ParentId = string.Empty;
        NodeType = 0;
        Guid ObjectId = Guid.Empty;

        string strCollNm = string.Empty;
        string strSumNm = "组数";
        string strAverageNm = "组均";

        //开始日期
        DateTime StartDate = new DateTime(2000, 1, 1);
        //结束日期
        DateTime EndDate = new DateTime(3000, 1, 1);
        if (context.Request.Params["StartDate"] == null && context.Request.Params["EndDate"] == null)
        {
            StartDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
            EndDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
        }
        else
        {
            //检索开始日期
            strParam = context.Request.Params["StartDate"];
            if (!string.IsNullOrEmpty(strParam))
            {
                StartDate = DateTime.Parse(strParam);
            }
            //检索结束日期
            strParam = context.Request.Params["EndDate"];
            if (!string.IsNullOrEmpty(strParam))
            {
                EndDate = DateTime.Parse(strParam + " 23:59:59");
            }
        }
        /*
        //检索开始日期
        strParam = context.Request.Params["StartDate"];
        if (!string.IsNullOrEmpty(strParam))
        {
            StartDate = DateTime.Parse(strParam);
        }
        //检索结束日期
        strParam = context.Request.Params["EndDate"];
        if (!string.IsNullOrEmpty(strParam))
        {
            EndDate = DateTime.Parse(strParam + " 23:59:59");
        }*/

        ParentType = context.Request.Params["ParentType"];
        ParentId = context.Request.Params["ParentId"];

        //显示子节点数据
        if (!string.IsNullOrEmpty(ParentType))
        {
            if (ParentType.Equals("0"))
            {
                NodeType = 10;
                strCollNm = "公司";
            }
            else if (ParentType.Equals("10"))
            {
                NodeType = 20;
                strCollNm = "商圈";
            }
            else if (ParentType.Equals("20"))
            {
                NodeType = 30;
                strCollNm = "部门";
            }
            else if (ParentType.Equals("30") || ParentType.Equals("25"))
            {
                NodeType = 40;
                strCollNm = "人员";
            }

            ObjectId = new Guid(ParentId);
        }
        //初始显示
        else
        {
            //权限判断
            //全权限
            if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewAllArchivement))
            {
                NodeType = 0;
                strCollNm = "城市";
            }
            //城市权限
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCityArchivement))
            {
                NodeType = 10;
                ObjectId = CurrentUserInfo.CityId;
                strCollNm = "公司";
            }
            //公司权限
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCompanyArchivement))
            {
                NodeType = 20;
                ObjectId = CurrentUserInfo.CompanyId;
                strCollNm = "商圈";
            }
            //部门权限
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewDepartArchivement))
            {
                NodeType = 25;
                ObjectId = CurrentUserInfo.UserId;
                strCollNm = "部门";
            }
            //个人权限
            else
            {
                NodeType = 50;
                ObjectId = CurrentUserInfo.UserId;
                strCollNm = "个人";
            }
        }

        //营业动态业务类
        Chenxin.Lib.House.BLL.House_DataCollection bllColl = new Chenxin.Lib.House.BLL.House_DataCollection();
        //汇总数据
        System.Data.DataSet dsColl = bllColl.GetCollection(NodeType, ObjectId, StartDate, EndDate);

        return dsColl.Tables[0];
        
    }

    /// <summary>
    /// 生成PIE报表
    /// </summary>
    /// <param name="context"></param>
    private void GetPieDB(HttpContext context)
    {
        int nodeType = 0;
        DataTable dt = GetData(context, ref nodeType);
        string business = "";
        string total = "";
        string rent = "";
        string avg = "";
        string column = "";
        string title = "";

        string type = context.Request["charttype"].ToLower();
        switch (type)
        {
            case "newhouse":
                business = "CNT_HOUSE_SALE";
                total = "CNT_HOUSE_TOTAL";
                rent = "CNT_HOUSE_RENT";
                avg = "AVG_HOUSE";
                title = "开盘房源报表";
                break;
            case "housetrace":
                business = "CNT_HOUSETRACE_SALE";
                total = "CNT_HOUSETRACE_TOTAL";
                rent = "CNT_HOUSETRACE_RENT";
                avg = "AVG_HOUSETRACE";
                title = "房源跟单报表";
                break;
            case "newcustomer":
                business = "CNT_DEMAND_SALE";
                total = "CNT_DEMAND_TOTAL";
                rent = "CNT_DEMAND_RENT";
                avg = "AVG_DEMAND";
                title = "客户需求报表";
                break;
            case "customertrace":
                business = "CNT_DEMANDTRACE_SALE";
                total = "CNT_DEMANDTRACE_TOTAL";
                rent = "AVG_DEMANDTRACE";
                avg = "AVG_DEMANDTRACE";
                title = "客户跟单报表";
                break;
            case "viewhouse":
                business = "CNT_VIEWHOUSE";
                rent = "AVG_VIEWHOUSE";
                title = "客户跟单报表";
                break;
            default:
                business = "CNT_HOUSE_SALE";
                total = "CNT_HOUSE_TOTAL";
                rent = "CNT_HOUSE_RENT";
                avg = "AVG_HOUSE";
                title = "带看报表，未实现";
                break;
        }

        switch (context.Request["column"].ToLower())
        {
            case "a":
                column = avg;
                title = "组均";
                break;
            case "b":
                column = business;
                if (type == "viewhouse")
                {
                    title = "带看";
                }
                else
                {
                    title = "买卖";
                }
                
                break;
            case "r":
                column = rent;
                if (type == "viewhouse")
                {
                    title = "平均";
                }
                else
                {
                    title = "租赁";
                }
                break;
            case "t":
                column = total;
                title = "合计";
                break;
                
                
        }

        OpenFlashChart.OpenFlashChart chart = new OpenFlashChart.OpenFlashChart();
        chart.Title = new Title(title);

        
        OpenFlashChart.Pie pie = new OpenFlashChart.Pie();
        
        List<PieValue> values = new List<PieValue>();
        List<string> labels = new List<string>();
        foreach (DataRow dr in dt.Rows)
        {
            labels.Add(dr["ChildName"].ToString());
            PieValue _v = new PieValue(Convert.ToDouble(dr[column]));
            _v.Text = dr["ChildName"].ToString() + ":" + _v.Value.ToString();
            _v.Click = "show(\"" + dr["ChildId"].ToString() + "\"," + nodeType + ",\"" + dr["ChildName"].ToString() + "\")";
            values.Add(_v);
        }
             
        pie.Values = values;
        pie.FontSize = 12;
        //pie.Alpha = .5;
        
        PieAnimationSeries pieAnimationSeries = new PieAnimationSeries();
        pieAnimationSeries.Add(new PieAnimation("bounce", 5));
        pie.Animate = pieAnimationSeries;
        //pie.GradientFillMode = true;

        //pie.FillAlpha = 1;

        //pie.Colour = "#fff";
        pie.Colours = new string[] {"#006600", "#000099", "#ff3300","#660000"};
        pie.Tooltip = "#label#条：共#total#条 占比#percent#";
        
        chart.AddElement(pie);
        chart.Bgcolor = "#ffffff";
        context.Response.Write(chart.ToPrettyString());
    }
    
    /// <summary>
    /// 生成比例效果的柱状报表
    /// </summary>
    /// <param name="context"></param>
    private void GetGlassBarDB(HttpContext context)
    {
        int nodeType = 0;
        DataTable dt = GetData(context,ref nodeType);
        string business = "";
        string total = "";
        string rent = "";
        string avg = "";
        string title = "开盘房源";
        
        string type=context.Request["charttype"].ToLower();
        switch (type)
        {
            case "newhouse":
                business = "CNT_HOUSE_SALE";
                total = "CNT_HOUSE_TOTAL";
                rent = "CNT_HOUSE_RENT";
                avg = "AVG_HOUSE";
                title = "开盘房源报表";
                break;
            case "housetrace":
                business = "CNT_HOUSETRACE_SALE";
                total = "CNT_HOUSETRACE_TOTAL";
                rent = "CNT_HOUSETRACE_RENT";
                avg = "AVG_HOUSETRACE";
                title = "房源跟单报表";
                break;
            case "newcustomer":
                business = "CNT_DEMAND_SALE";
                total = "CNT_DEMAND_TOTAL";
                rent = "CNT_DEMAND_RENT";
                avg = "AVG_DEMAND";
                title = "客户需求报表";
                break;
            case "customertrace":
                business = "CNT_DEMANDTRACE_SALE";
                total = "CNT_DEMANDTRACE_TOTAL";
                rent = "AVG_DEMANDTRACE";
                avg = "AVG_DEMANDTRACE";
                title = "客户跟单报表";
                break;
            case "viewhouse":
                business = "CNT_VIEWHOUSE";
                total = "AVG_VIEWHOUSE";
                title = "带看报表";
                break;
            default:
                business = "CNT_HOUSE_SALE";
                total = "CNT_HOUSE_TOTAL";
                rent = "CNT_HOUSE_RENT";
                avg = "AVG_HOUSE";
                title = "带看报表，未实现";
                break;
        }
        OpenFlashChart.OpenFlashChart chart = new OpenFlashChart.OpenFlashChart();

        BarGlass bBar = new BarGlass();//买卖
        BarGlass tBar = new BarGlass();//合计
        BarGlass rBar = new BarGlass();//租赁
        BarGlass aBar = new BarGlass();//组均

        bBar.FillAlpha = 0.8;
        bBar.FontSize = 12;
        bBar.Colour = "#0072E3";
        bBar.Set_Key("买卖", 12);

        tBar.FillAlpha = 0.8;
        tBar.FontSize = 12;
        tBar.Colour = "#AE0000";
        tBar.Set_Key("合计", 12);

        rBar.FillAlpha = 0.8;
        rBar.FontSize = 12;
        rBar.Colour = "#64A600";
        rBar.Set_Key("租赁", 12);

        aBar.FillAlpha = 0.8;
        aBar.FontSize = 12;
        aBar.Colour = "#333333";
        aBar.Set_Key("组均", 12);


        if (type == "viewhouse")
        {
            bBar.Set_Key("带看", 12);
            tBar.Set_Key("平均", 12);            
        }
        
        List<BarGlassValue> bValue = new List<BarGlassValue>();
        List<BarGlassValue> tValue = new List<BarGlassValue>();
        List<BarGlassValue> rValue = new List<BarGlassValue>();
        List<BarGlassValue> aValue = new List<BarGlassValue>();

        List<string> labels = new List<string>();

        int max = 1;
        foreach (DataRow dr in dt.Rows)
        {
            labels.Add(dr["ChildName"].ToString());

            BarGlassValue _bhv = new BarGlassValue(Convert.ToDouble(dr[business]));
            bValue.Add(_bhv);

            if (max < Convert.ToInt32(dr[business]))
            {
                max = Convert.ToInt32(dr[business]);
            }


            BarGlassValue _thv = new BarGlassValue(Convert.ToDouble(dr[total]));
            tValue.Add(_thv);

            if (max < Convert.ToInt32(dr[total]))
            {
                max = Convert.ToInt32(dr[total]);
            }


            if (type != "viewhouse")
            {
                BarGlassValue _rhv = new BarGlassValue(Convert.ToDouble(dr[rent]));
                rValue.Add(_rhv);

                if (max < Convert.ToInt32(dr[rent]))
                {
                    max = Convert.ToInt32(dr[rent]);
                }

                BarGlassValue _ahv = new BarGlassValue(Convert.ToDouble(dr[avg]));
                aValue.Add(_ahv);

                if (max < Convert.ToInt32(dr[avg]))
                {
                    max = Convert.ToInt32(dr[avg]);
                }
            }
        }
        chart.Title = new Title(title);
        bBar.Values = bValue;        
        tBar.Values = tValue;
        rBar.Values = rValue;
        aBar.Values = aValue;

        bBar.Tooltip = "#key#:#val#";
        tBar.Tooltip = "#key#:#val#";
        rBar.Tooltip = "#key#:#val#";
        aBar.Tooltip = "#key#:#val#";


        if (type != "viewhouse")
        {
            chart.AddElement(bBar);
            chart.AddElement(tBar);
            chart.AddElement(rBar);
            chart.AddElement(aBar);
        }
        else
        {
            chart.AddElement(bBar);
            chart.AddElement(tBar);            
        }

        XAxis xaxis = new XAxis();
        xaxis.Labels.SetLabels(labels);
        xaxis.Offset = true;
        xaxis.Labels.FontSize = 12;
        chart.X_Axis = xaxis;

        YAxis yaxis = new YAxis();

        int step = 1;
        if (max > 10)
        {
            step = (int)(max / 10);
        }
        yaxis.Steps = step;
        yaxis.SetRange(0, max + 10);

        chart.Y_Axis = yaxis;

        context.Response.Write(chart.ToPrettyString());
    }
}